##########################################################################################

library(dplyr)
library(data.table)
library(optparse)
library(ggplot2)
library(Scissor)

##########################################################################################
option_list <- list(
    make_option(c("--input_im_file"), type = "character"),
    make_option(c("--single_cell_file"), type = "character"),
    make_option(c("--gene"), type = "character"),
    make_option(c("--type"), type = "character"),
    make_option(c("--out_path"), type = "character") 
)

if(1!=1){
    
    ## 韩国的单细胞表达文件
    input_im_file <- "~/20220915_gastric_multiple/dna_combinePublic/images/singleCell_TPM_ourData/MUC6_1.5/Scissor_STAD_MUC6_mutation.IM.CellRate.all.tsv"
    single_cell_file <- "~/20220915_gastric_multiple/dna_combinePublic/images/singleCell_TPM_ourData/MUC6_1.5/Scissor_STAD_MUC6_mutation.IM.CellRate.all.RData"
    gene <- "MUC6"
    type <- "all"
    ## 输出
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/images/singleCell_TPM_ourData/MUC6_1.5/"

}

###########################################################################################
parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

input_im_file <- opt$input_im_file
single_cell_file <- opt$single_cell_file
out_path <- opt$out_path
gene <- opt$gene
type <- opt$type

dir.create(out_path , recursive = T)

###########################################################################################

dat_im <- data.frame(fread(input_im_file))

cell_order <- dat_im %>%
group_by( Cell_Type ) %>%
summarize( Cells = sum(Cells) )

tmp <- load(single_cell_file)

###########################################################################################

cell_order <- unique(data.frame(cell_order)[order(cell_order$Cells , decreasing=T),"Cell_Type"])

tmp_data <- dat_im
tmp_data$Scissor_Type <- as.character(tmp_data$Scissor_Type)
tmp_data$Cell_Type <- factor( tmp_data$Cell_Type , levels = cell_order , order = T )

## 去除主细胞,该细胞数量太少且有MUC6突变定义不明确，以访误导
tmp_data <- subset( tmp_data , Cell_Type != "Tumor" )
col <- c('grey','indianred1','royalblue')
names(col) <- c("Background" , "Scissor+" , "Scissor-" )

#tmp_data <- subset(tmp_data , Scissor_Type!="Background")
p1 <- ggplot(tmp_data,aes(x=Cell_Type,y=Cells,fill=factor(Scissor_Type))) +
    geom_bar(stat="identity") +
    ylab("Cell counts") +
    xlab(NULL) +
    theme_bw() +
    theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 10,color="black",face='bold'),
        legend.position = c(0.7,0.7) ,
        axis.text.x = element_text(size = 12,color="black",face='bold' , angle = 45, hjust = 1),
        axis.text.y = element_text(size = 10,color="black",face='bold'),
        axis.title.x = element_text(size = 10,color="black",face='bold'),
        axis.title.y = element_text(size = 13,color="black",face='bold'),
        strip.text.x = element_text(size = 15,color="black",face='bold'),
        axis.ticks.length = unit(0.2, "cm") ,
        axis.line = element_line(size = 0.5))  +
    scale_fill_manual(values=c(col))

out_name <- paste0( out_path , "/Scissor_STAD_" , gene , "_mutation.IM.CellRate.all.Counts.pdf"  )
ggsave(file=out_name,plot=p1,width=4,height=4)


###########################################################################################
#sc_dataset$scissor[which(sc_dataset$scissor==0)] <- "Background"
#sc_dataset$scissor[which(sc_dataset$scissor==1)] <- "Scissor+"
#sc_dataset$scissor[which(sc_dataset$scissor==2)] <- "Scissor-"
#sc_dataset$scissor <- factor( sc_dataset$scissor , levels = c("Background" , "Scissor+" , "Scissor-") , order = T )

#print(levels(sc_dataset$scissor))

sc_dataset <- subset( sc_dataset , celltype != "Tumor" )

p1 <- DimPlot(sc_dataset, reduction = 'umap', group.by = 'scissor', cols = c('grey','indianred1','royalblue'), pt.size = 1.2, order = c(2,1)) +
    theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 12,color="black",face='bold'),
        legend.position = 'right' ,
        axis.text.x = element_text(size = 10,color="black",face='bold'),
        axis.text.y = element_text(size = 10,color="black",face='bold'),
        axis.title.x = element_text(size = 10,color="black",face='bold'),
        axis.title.y = element_text(size = 13,color="black",face='bold'),
        strip.text.x = element_text(size = 15,color="black",face='bold'),
        axis.ticks.length = unit(0.2, "cm") ,
        axis.line = element_line(size = 0.5))
#out_name <- paste0( out_path , "/Scissor_STAD_" , gene , "_mutation.",class,".cellType.",mut_type,".pdf"  )

p2 <- DimPlot(sc_dataset, reduction = 'umap', group.by = 'celltype' , pt.size = 1.2, order = c(2,1)) +
    theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 12,color="black",face='bold'),
        legend.position = 'right' ,
        axis.text.x = element_text(size = 10,color="black",face='bold'),
        axis.text.y = element_text(size = 10,color="black",face='bold'),
        axis.title.x = element_text(size = 10,color="black",face='bold'),
        axis.title.y = element_text(size = 13,color="black",face='bold'),
        strip.text.x = element_text(size = 15,color="black",face='bold'),
        axis.ticks.length = unit(0.2, "cm") ,
        axis.line = element_line(size = 0.5))

plot <- p1 + p2
out_name <- paste0( out_path , "/Scissor_STAD_" , gene , "_mutation.IM.all.cell.pdf"  )
ggsave(file=out_name,plot=plot,width=10,height=4.5)
